package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.AuthorizedPatentsDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.AuthorizedPatents;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLAuthorizedPatentInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.AuthorizedPatentInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddAuthorizedPatentMutation implements DataFetcher {

    @Autowired
    AuthorizedPatentsDao authorizedPatentsDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;


    @Autowired
    TokenService tokenService;
    /**
     * 添加获得授权专利情况
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLAuthorizedPatentInput input = AuthorizedPatentInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        AuthorizedPatents authorizedPatents = null;

        if (input.getId() != null) {
            authorizedPatents = authorizedPatentsDao.get(AuthorizedPatents.class, input.getId());
            authorizedPatents.setUpdatedAt(new Date());
        } else {
            authorizedPatents = new AuthorizedPatents();
            authorizedPatents.setCreatedAt(new Date());
        }
        if(schoolId != null){

            authorizedPatents.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            authorizedPatents.setTeacher_id(input.getTeacher_id());
        }
        if(input.getType() != null){

            authorizedPatents.setType(input.getType());
        }
        if(input.getImpowerdate() != null){

            authorizedPatents.setImpowerdate(input.getImpowerdate());
        }
        if(input.getImage() != null){

            authorizedPatents.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            authorizedPatents.setShenhe(input.getShenhe());
        }
        if(input.getName() != null){

            authorizedPatents.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getImpowernumber() != null){

            authorizedPatents.setImpowernumber(JSON.toJSONString(input.getImpowernumber()));
        }
        if(input.getInventor() != null){

            authorizedPatents.setInventor(JSON.toJSONString(input.getInventor()));
        }
        if(input.getNation() != null){

            authorizedPatents.setNation(JSON.toJSONString(input.getNation()));
        }
        try{
            authorizedPatentsDao.saveOrUpdate(authorizedPatents);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

